package com.xianggu.agency.demo;

import com.xianggu.agency.jdkProxy.Compiler;
import org.springframework.core.io.DefaultResourceLoader;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Files;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author chengjy
 * @date 2025/3/18
 */
public class MyInterfaceFactory {

    private ClassLoader classLoader = new DefaultResourceLoader().getClassLoader();

    private String packageName = "com.xianggu.insight.proxy";

    String createPath = "D:\\临时文件夹呀呀呀呀\\动态代理生成文件";

    private static AtomicInteger atomicInteger = new AtomicInteger(0);

    private File createJavaFile(Class aClass, MyHandler handler) throws IOException {
        String fileName = getClassName(aClass);

        String func1 = handler.function("func1", "");
        String func2 = handler.function("func2", "");
        String func3 = handler.function("func3", "");

        String context ="package "+ packageName +"; \n"+
                "\n" +
                "import "+ aClass.getName() +";\n" +
                "\n" +
                "/**\n" +
                " * @author chengjy\n" +
                " * @date 2025/3/18\n" +
                " */\n" +
                "public class "+fileName+" implements "+ aClass.getSimpleName() +" {\n" +
                "    private MyInterface myInterface;\n" +
                "    @Override\n" +
                "    public void func1() {\n" +
                func1+"\n" +
                "    }\n" +
                "\n" +
                "    @Override\n" +
                "    public void func2() {\n" +
                func2+"\n" +
                "\n" +
                "    }\n" +
                "\n" +
                "    @Override\n" +
                "    public void func3() {\n" +
                func3+"\n" +
                "    }\n" +
                "}\n";
        File file = new File( createPath + "\\" + fileName + ".java");
        Files.writeString(file.toPath(), context);
        return file;
    }

    private String getClassName(Class aClass) throws IOException {
        return aClass.getSimpleName()+ "$proxy0" + atomicInteger.incrementAndGet();
    }

    private Object createJava(String fileName) throws IOException {
        try {
            if (fileName.contains(".java")) {
                fileName = fileName.replace(".java", "");
            }

            Class<?> aClass = classLoader.loadClass(fileName);
            Constructor<?>[] constructors = aClass.getConstructors();
            constructors[0].setAccessible(true);
            return constructors[0].newInstance();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }

    public static Object createProxyObject(Class<?> tClass , MyHandler handler) throws IOException {
        // 创建一个被代理对象
        MyInterfaceFactory myInterfaceFactory = new MyInterfaceFactory();
        File javaFile = myInterfaceFactory.createJavaFile(tClass , handler);

        Compiler.complie(javaFile);
        MyInterface object = (MyInterface) myInterfaceFactory.createJava(myInterfaceFactory.packageName + "." + javaFile.getName());
        handler.setPorxy(object);
        return object;
    }

}
